package com.csu.cphone.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.vo.ResultVo;
import com.csu.cphone.pojo.RecycleProblemPojo;
import com.csu.cphone.pojo.RecycleProblemSelectPojo;
import com.csu.cphone.service.RecycleProblemService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：cphone
 * @类名称：RecycleProblemManager
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年12月3日 下午9:50:15
 * @company:
 */
@Controller
@RequestMapping("admin/recycle/problem")
@Api(value = "admin", description = "管理后台")
public class RecycleProblemManager
{
    @Autowired
    private RecycleProblemService recycleProblemService;

    @ResponseBody
    @ApiOperation(value = "添加", notes = "添加数据")
    @RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<RecycleProblemPojo> add(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "手机id") @RequestParam(value = "phoneId", required = true) Integer phoneId,
            @ApiParam(value = "问题类型名称") @RequestParam(value = "problemName", required = true) String problemName,
            @ApiParam(value = "题目类型（0：单选，1：多选）") @RequestParam(value = "problemType", required = true) Integer problemType,
            @RequestBody(required = false) List<RecycleProblemSelectPojo> selects)
    {

        ResultVo<RecycleProblemPojo> resultVo = new ResultVo<RecycleProblemPojo>();
        // 添加
        RecycleProblemPojo result = recycleProblemService.add(phoneId, problemName, problemType, selects);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "批量添加", notes = "添加数据")
    @RequestMapping(value = "/batchAdd", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<RecycleProblemPojo>> batchAdd(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "手机id") @RequestParam(value = "phoneId", required = true) Integer phoneId,
            @RequestBody(required = true) List<RecycleProblemPojo> problems)
    {

        ResultVo<List<RecycleProblemPojo>> resultVo = new ResultVo<List<RecycleProblemPojo>>();
        // 添加
        List<RecycleProblemPojo> result = recycleProblemService.batchAdd(phoneId, problems);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "更新", notes = "更新数据，返回1成功")
    @RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> update(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "问题id") @RequestParam(value = "id", required = true) Integer id,
            @ApiParam(value = "手机id") @RequestParam(value = "phoneId", required = true) Integer phoneId,
            @ApiParam(value = "问题类型名称") @RequestParam(value = "problemName", required = true) String problemName,
            @ApiParam(value = "题目类型（0：单选，1：多选）") @RequestParam(value = "problemType", required = true) Integer problemType,
            @RequestBody(required = false) List<RecycleProblemSelectPojo> selects)
    {

        ResultVo<Integer> resultVo = new ResultVo<Integer>();
        // 添加
        Integer result = recycleProblemService.update(id, phoneId, problemName, problemType, selects);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "批量更新", notes = "更新数据，返回1成功")
    @RequestMapping(value = "/batchUpdate", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> batchUpdate(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "手机id") @RequestParam(value = "phoneId", required = true) Integer phoneId,
            @RequestBody(required = true) List<RecycleProblemPojo> problems

    )
    {
        ResultVo<Integer> resultVo = new ResultVo<Integer>();
        // 添加
        Integer result = recycleProblemService.batchUpdate(phoneId, problems);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询", notes = "单个")
    @RequestMapping(value = "/queryById", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<RecycleProblemPojo> queryById(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "回收问题id") @RequestParam(value = "recycleProblemId", required = true) Integer recycleProblemId)
    {

        ResultVo<RecycleProblemPojo> resultVo = new ResultVo<RecycleProblemPojo>();
        // 添加
        RecycleProblemPojo result = recycleProblemService.queryById(recycleProblemId);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询总数", notes = "总数")
    @RequestMapping(value = "/count", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> count(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "回收手机id") @RequestParam(value = "recyclePhoneId", required = false) Integer recyclePhoneId)
    {
        ResultVo<Integer> resultVo = new ResultVo<Integer>();
        // 添加
        Integer result = recycleProblemService.count(recyclePhoneId);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询列表", notes = "分页")
    @RequestMapping(value = "/queryList", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<RecycleProblemPojo>> queryList(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "页面大小") @RequestParam(value = "pageSize", required = true) Integer pageSize,
            @ApiParam(value = "开始位置") @RequestParam(value = "startIndex", required = true) Integer startIndex,
            @ApiParam(value = "排序") @RequestParam(value = "order", required = false) String order,
            @ApiParam(value = "回收手机id") @RequestParam(value = "recyclePhoneId", required = false) Integer recyclePhoneId)
    {

        ResultVo<List<RecycleProblemPojo>> resultVo = new ResultVo<List<RecycleProblemPojo>>();
        // 添加
        if (!"DESC".equalsIgnoreCase(order))
        {
            order = "ASC";
        }
        List<RecycleProblemPojo> result = recycleProblemService.queryList(pageSize, startIndex, order, recyclePhoneId);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "删除", notes = "删除数据")
    @RequestMapping(value = "/delById", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> deleteById(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "回收问题id") @RequestParam(value = "recycleProblemId", required = true) Integer recycleProblemId)
    {

        ResultVo<Integer> resultVo = new ResultVo<Integer>();
        // 添加
        Integer result = recycleProblemService.deleteById(recycleProblemId);
        resultVo.setData(result);

        return resultVo;
    }
}
